//
//  ViewController.m
//  scaleJPG
//
//  Created by ran jingfu on 2016/12/27.
//  Copyright © 2016年 ran jingfu. All rights reserved.
//

#import "ViewController.h"


@interface ViewController ()

@property(nonatomic,weak)IBOutlet  NSButton   *btnStart;

@end

@implementation ViewController


- (IBAction)clickStart:(id)sender
{
    
    [NSThread detachNewThreadWithBlock:^{
        
        NSString  *fileDir = @"/Users/ranjingfu/Desktop/出差资料/无锡巡检影像";
        
        NSArray *arrayFile = [[NSFileManager defaultManager] subpathsAtPath:fileDir];
        
        for (NSString  *fileName in arrayFile) {
            
            if ([fileName hasSuffix:@".JPG"] || [fileName hasSuffix:@".jpg"]) {
                
                NSString *strName = [fileDir stringByAppendingPathComponent:fileName];
                [self write:strName];
                
                //  NSLog(@"fileName:%@",fileName);
            }
            
        }
        
        NSLog(@"**********************end**********************");
    }];
}

- (BOOL)write:(NSString*)strFilePath
{
    
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:strFilePath];
  //  CGFloat fminWidth = fmin(image.size.width, image.size.height);
//    if (fminWidth < 3000) {
//        return NO;
//    }
   
    NSData *imageData = [image TIFFRepresentation];
    if (imageData.length < 2*1024*1024*8) {
        return NO;
    }
    
   // image = [self createScalesImage:image];
   // imageData = [image TIFFRepresentationUsingCompression:NSTIFFCompressionJPEG factor:0.5];
    
    NSBitmapImageRep *imageRep = [self bitmapImageRepresentation:image];
   // [imageRep setSize:CGSizeMake(image.size.width/2, image.size.height/2)];
    NSDictionary *imageProps = nil;
    NSNumber *quality = [NSNumber numberWithFloat:0.85];
    imageProps = [NSDictionary dictionaryWithObject:quality forKey:NSImageCompressionFactor];
    NSData *imageData1 = [imageRep representationUsingType:NSJPEGFileType properties:imageProps];
    BOOL suc = [[NSFileManager defaultManager] removeItemAtPath:strFilePath error:nil];
    if (suc) {
        strFilePath = [strFilePath stringByReplacingOccurrencesOfString:@".JPG" withString:@".jpg"];
        BOOL write = [imageData1 writeToFile:strFilePath atomically:YES];
        if (write) {
            NSLog(@"write %@ suc",strFilePath);
        }else{
            NSLog(@"write %@ fail",strFilePath);

        }
    }else{
        
        NSLog(@"remove %@ fail",strFilePath);
    }
    
    return YES;
}



//- (NSBitmapImageRep *)bitmapImageRepresentation:(NSImage*)image {
//    int width = [image size].width;
//    int height = [image size].height;
//    
//    if(width < 1 || height < 1)
//        return nil;
//    NSData *imageData = [image TIFFRepresentation];
//    NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:imageData];
//    
//    NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep: rep];
//    [NSGraphicsContext saveGraphicsState];
//    [NSGraphicsContext setCurrentContext: ctx];
//    [image drawInRect:NSMakeRect(0, 0, width/2, height/2)];
//    // [image drawAtPoint: NSZeroPoint fromRect: NSZeroRect operation: NSCompositingOperationCopy fraction: 1.0];
//    // [image drawInRect:CGRectMake(0, 0, width/2, height/2) fromRect:CGRectZero operation:NSCompositingOperationCopy fraction:1 respectFlipped:YES hints:NULL];
//    [ctx flushGraphics];
//    [NSGraphicsContext restoreGraphicsState];
//    
//    return rep;
//}

- (NSBitmapImageRep *)bitmapImageRepresentation:(NSImage*)image {
    int width = [image size].width;
    int height = [image size].height;
    
    if(width < 1 || height < 1)
        return nil;
    
    NSBitmapImageRep *rep = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes: NULL
                             pixelsWide: width/2
                             pixelsHigh: height/2
                             bitsPerSample: 8
                             samplesPerPixel: 4
                             hasAlpha: YES
                             isPlanar: NO
                             colorSpaceName: NSDeviceRGBColorSpace
                             bytesPerRow: width*4
                             bitsPerPixel: 32];
    
    NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep: rep];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext: ctx];
    [image drawInRect:NSMakeRect(0, 0, width/2, height/2)];
   // [image drawAtPoint: NSZeroPoint fromRect: NSZeroRect operation: NSCompositingOperationCopy fraction: 1.0];
   // [image drawInRect:CGRectMake(0, 0, width/2, height/2) fromRect:CGRectZero operation:NSCompositingOperationCopy fraction:1 respectFlipped:YES hints:NULL];
    [ctx flushGraphics];
    [NSGraphicsContext restoreGraphicsState];
    
    return rep;
}



- (NSImage *)createScalesImage:(NSImage *)sourceImage
{
    //set flipped
    
    //source picture size
    NSSize srcSize = [sourceImage size];
    unsigned int uiWidth= srcSize.width;
    unsigned int uiHeight= srcSize.height;
    
    //target bg picture size
    unsigned int bgWidth = uiWidth/2;
    unsigned int bgHeight = uiHeight/2;
    NSSize tarSize =NSMakeSize(bgWidth, bgHeight);
    [sourceImage setScalesWhenResized:YES];
    [sourceImage setSize:tarSize];
    return [sourceImage copy];
    /*
    if(uiWidth>=bgWidth && uiHeight >= bgHeight)
    {
        [sourceImage setSize:tarSize];
        return [sourceImage copy];
    }
    if(uiWidth>bgWidth && uiHeight < bgHeight)
    {
        [sourceImage setSize:tarSize];
        
        //target bg picture
        NSImage *targetImage = [[NSImage alloc] initWithSize:tarSize];
        [targetImage lockFocus];
        //fill target bg picture,using white color
        [[NSColor whiteColor] set];
        NSRectFill(NSMakeRect(0,0, bgWidth, bgHeight*fraction));
        
        //draw
        [sourceImage drawAtPoint:NSMakePoint(0,(bgHeight - uiHeight)*0.5) fromRect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0];
        [targetImage unlockFocus];
        return [targetImage autorelease];
    }
    if(uiWidth<bgWidth && uiHeight >bgHeight)
    {
        [sourceImage setSize:tarSize];
        
        //target bg picture
        NSImage *targetImage = [[NSImage alloc] initWithSize:tarSize];
        [targetImage lockFocus];
        //fill target bg picture,using white color
        [[NSColor whiteColor] set];
        NSRectFill(NSMakeRect(0, 0, bgWidth, bgWidth*fraction));
        
        //draw
        [sourceImage drawAtPoint:NSMakePoint((bgWidth- uiWidth)*0.5, 0) fromRect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0];
        [targetImage unlockFocus];
        return [targetImage autorelease];
    }
    else
        //(uiWidth<bgWidth && uiHeight < bgHeight)
    {
        //[sourceImage setSize:tarSize];
        //target bg picture
        NSImage *targetImage = [[NSImage alloc] initWithSize:tarSize];
        [targetImage lockFocus];
        //fill target bg picture,using white color
        [[NSColor whiteColor] set];
        NSRectFill(NSMakeRect(0, 0, bgWidth, bgWidth*fraction));
        
        //draw
        [sourceImage drawAtPoint:NSMakePoint((bgWidth - uiWidth)*0.5, (bgHeight - uiHeight)*0.5) fromRect:NSZeroRect operation:NSCompositeSourceIn fraction:1.0];
        [targetImage unlockFocus];
        return [targetImage autorelease];
    }*/
}

- (void)viewDidLoad {
    [super viewDidLoad];
    

    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end
